// pages/result_year/result_year.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fold:true,
    calYear:"",
    annualSalary:"",
    yearEndBonus:"",
    annualBaochou:"",
    annualRoyalties:"",
    annualFranchise:"",
    yearEndBonusWayChange:{checkA:false,checkB:true},
    annualGongjijing:"",
    annualSecurate:"",
    annualZhuanXiang:"",
    annualAnother:"",

    totalAnnualTax:"0.00",
    annualBeforeTax:"0.00",
    annualAfterTax:"0.00",
    annualYEBTax:"0.00",
    annualTaxRate:"3%",
    annualSusuanKouchu:"0"
  },

  toggleFold(e){
    this.setData({fold:!this.data.fold})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const year = options.calYear;
    this.setData({calYear:year});
    let that = this;
    try {
      var value = wx.getStorageSync("annualBaochou");
        if(value){
        that.setData({annualBaochou:JSON.parse(value)});
        }
        value = wx.getStorageSync("annualFranchise");
        if(value){
        that.setData({annualFranchise:JSON.parse(value)});
        }
        value = wx.getStorageSync("annualRoyalties");
        if(value){
        that.setData({annualRoyalties:JSON.parse(value)});
        }
        value = wx.getStorageSync("yearEndBonus");
        if(value){
        that.setData({yearEndBonus:JSON.parse(value)});
        }
        value = wx.getStorageSync("annualSalary");
        if(value){
        that.setData({annualSalary:JSON.parse(value)});
        }
        value = wx.getStorageSync("yearEndBonusWayChange");
        if(value){
        that.setData({yearEndBonusWayChange:JSON.parse(value)});
        }
        value = wx.getStorageSync("annualSecurate");
        if(value){
          that.setData({annualSecurate:JSON.parse(value)});
          }
        value = wx.getStorageSync("annualGongjijing");
        if(value){
          that.setData({annualGongjijing:JSON.parse(value)});
          }
        value = wx.getStorageSync("annualZhuanXiang");
        if(value){
          that.setData({annualZhuanXiang:JSON.parse(value)});
        }
        value = wx.getStorageSync("annualAnother");
        if(value){
          that.setData({annualAnother:JSON.parse(value)});
        }
    } catch (e) {
      console.log(e)
    }
  },

  onBackToIndex(e){
    wx.navigateBack({
      delta: 1  // 返回上一级页面。
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  calAnnualZongheSuode:function(){
     var salary = (this.data.annualSalary === ""?0:Number(this.data.annualSalary));
     var baochou = (this.data.annualBaochou === ""?0:Number(this.data.annualBaochou));
     var royaltied = (this.data.annualRoyalties === ""?0:Number(this.data.annualRoyalties));
     var fra = (this.data.annualFranchise === ""?0:Number(this.data.annualFranchise));
     var yearEndBonus = (this.data.yearEndBonus === ""?0:Number(this.data.yearEndBonus))
     var YEBNum = (this.data.annualYEBTax === ""?0:Number(this.data.annualYEBTax));
     var checked = this.data.yearEndBonusWayChange;
     var salNum = salary + baochou + royaltied + fra + yearEndBonus;
     //checkA表示合并计税，将年终奖加入综合所得里，否则不算
     if(checked.checkA){
       salNum = salNum + YEBNum;
     }
     this.setData({annualBeforeTax:salNum});
     // 工资薪金为0，个税为0
     return salNum;
  },

  //计算本年度应纳税额
  calAnnualYingNaShuiE:function(){
    var zonghesuode = this.data.annualBeforeTax;
    var securate = (this.data.annualSecurate === ""?0:Number(this.data.annualSecurate));
    var zhuanxiang =  (this.data.annualZhuanXiang === ""?0:Number(this.data.annualZhuanXiang));
    var housingFund = (this.data.annualGongjijing === ""?0:Number(this.data.annualGongjijing));
    var another = (this.data.annualAnother === ""?0:Number(this.data.annualAnother));
    var yingNa = (zonghesuode - 5000 * 12 - securate - zhuanxiang - housingFund - another).toFixed(2);
    yingNa = yingNa > 0?yingNa:0;
    return yingNa;
  },
 
  calYEBTax:function(){
    var YEBNum = (this.data.yearEndBonus === ""?0:Number(this.data.yearEndBonus));
    var tax = 0;
    if(YEBNum <= 3000){
      tax = (YEBNum * 0.03).toFixed(2)
      this.setData({annualYEBTax:tax});
    }else if(YEBNum > 3000 && YEBNum <= 12000){
      tax = (YEBNum * 0.1 -210).toFixed(2)
      this.setData({annualYEBTax:tax});
    }else if(YEBNum > 12000 && YEBNum <= 25000){
      tax = (YEBNum * 0.2 - 1410).toFixed(2)
      this.setData({annualYEBTax:tax});
    }else if(YEBNum > 25000 && YEBNum <= 35000){
      tax = (YEBNum * 0.25 - 2660).toFixed(2)
      this.setData({annualYEBTax:tax});
    }else if(YEBNum > 35000 && YEBNum <= 55000){
      tax = (YEBNum * 0.3 - 4410).toFixed(2)
      this.setData({annualYEBTax:tax});
    }else if(YEBNum > 55000 && YEBNum <= 80000){
      tax = (YEBNum * 0.35 - 7160).toFixed(2)
      this.setData({annualYEBTax:tax});
    }else{
      tax = (YEBNum * 0.45 - 15160).toFixed(2)
      this.setData({annualYEBTax:tax});
    }
  },
  // 计算年度个税
  calAnnualTax:function(){
    this.calYEBTax();
    var yebTax = this.data.annualYEBTax;
    var checked = this.data.yearEndBonusWayChange;
    var yingNa = this.calAnnualYingNaShuiE();
    var zonghesuode = this.data.annualBeforeTax;
    var tax = 0,afterTax = 0;
    //单独计税
    if(checked.checkB){
      tax = Number(yebTax);
    }
    if(yingNa <= 36000){
      tax =  ( tax + yingNa * 0.03).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax});
      return;
    }else if(yingNa > 36000 && yingNa <= 144000){
      tax = (tax + yingNa * 0.1 - 2520).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax,
      annualTaxRate:"10%",annualSusuanKouchu:"2520"});
      return;
    }else if(yingNa > 144000 && yingNa <= 300000){
      tax = (tax + yingNa * 0.2 - 16920).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax,
      annualTaxRate:"20%",annualSusuanKouchu:"16920"});
      return;
    }else if(yingNa > 300000 && yingNa <= 420000){
      tax = (tax + yingNa * 0.25 - 31920).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax,
      annualTaxRate:"25%",annualSusuanKouchu:"31920"});
      return;
    }else if(yingNa > 420000 && yingNa <= 660000){
      tax = (tax + yingNa * 0.3 - 52920).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax,
      annualTaxRate:"30%",annualSusuanKouchu:"52920"});
      return;
    }else if(yingNa > 660000 && yingNa <= 960000){
      tax = (tax + yingNa * 0.35 - 85920).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax,
      annualTaxRate:"35%",annualSusuanKouchu:"85920"});
      return;
    }else{
      tax = (tax + yingNa * 0.45 - 181920).toFixed(2);
      afterTax = zonghesuode - tax;
      this.setData({totalAnnualTax:tax,annualAfterTax:afterTax,
      annualTaxRate:"45%",annualSusuanKouchu:"181920"});
    }
    return;
  },

  onShow() {
    this.calAnnualZongheSuode();
    this.calAnnualTax();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '答哥算税计算器',
      path: '/pages/index/index'
    }
  }
})